package com.zyq.controller;

import com.zyq.model.common.ResponseBean;
import com.zyq.model.entity.Field;
import com.zyq.model.entity.HealthAssessment;
import com.zyq.service.IHealthAssessmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 健康评估表Controller
 *
 * @author zyq
 * @date 2021-12-08
 */
@Api( tags = "健康评估表接口")
@RestController
@RequestMapping("/healthAssessment")
public class HealthAssessmentController
{
    @Autowired
    private IHealthAssessmentService healthAssessmentService;

    /**
     * 查看健康评估表列表
     */
    @ApiOperation("用户查看健康评估表列表")
    @GetMapping("/showAll")
    public ResponseBean showAll()
    {
        return new ResponseBean(HttpStatus.OK.value(),
                "查询成功(Query was successful)",
                healthAssessmentService.selectHealthAssessmentMap());
    }

    /**
     * 修改字段
     * @param Authorization
     * @return
     */
    @RequiresRoles("管理员")
    @ApiOperation("管理员：修改字段")
    @PutMapping("/updateField")
    public ResponseBean edit(@RequestBody Field field, @RequestHeader String Authorization)
    {
        if(healthAssessmentService.updateField(field)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 新增字段
     */
    @ApiOperation("管理员：新增字段")
    @PostMapping("/insertField")
    public ResponseBean insertField(@RequestBody Field field, @RequestHeader String Authorization)
    {
        if(healthAssessmentService.insertField(field)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 删除字段
     */
    @ApiOperation("管理员：删除字段")
    @DeleteMapping("/field/{ids}")
    public ResponseBean removeField(@PathVariable Long[] ids)
    {
        if(healthAssessmentService.removeField(ids)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 管理员：查询字段
     * @param Authorization
     * @return
     */
    @RequiresRoles("管理员")
    @GetMapping("/listField")
    @ApiOperation("管理员：查询字段")
    public ResponseBean listField(@RequestHeader String Authorization)
    {
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("data", healthAssessmentService.selectFieldList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }


    @RequiresRoles("管理员")
    @GetMapping("/listOption")
    @ApiOperation("管理员：根据字段查选项")
    public ResponseBean listDetail(Long fieldId,@RequestHeader String Authorization)
    {
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("data", healthAssessmentService.selectHealthAssessmentList(fieldId));
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    /**
     * 新增健康评估表字段信息
     */
    @ApiOperation("管理员：新增选项及得分")
    @PostMapping("/insertOption")
    public ResponseBean add(@RequestBody HealthAssessment healthAssessment,@RequestHeader String Authorization)
    {
        if(healthAssessmentService.insertHealthAssessment(healthAssessment)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 修改健康评估表
     */
    @RequiresRoles("管理员")
    @ApiOperation("管理员：修改选项or得分")
    @PutMapping("/updateOption")
    public ResponseBean edit(@RequestBody HealthAssessment healthAssessment,@RequestHeader String Authorization)
    {
        if(healthAssessmentService.updateHealthAssessment(healthAssessment)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }

    /**
     * 删除健康评估表
     */
    @ApiOperation("管理员：删除选项")
    @DeleteMapping("/option/{ids}")
    public ResponseBean remove(@PathVariable Long[] ids,@RequestHeader String Authorization)
    {
        if(healthAssessmentService.deleteHealthAssessmentByIds(ids)>0){
            return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
        }
        return new ResponseBean(HttpStatus.OK.value(), "操作失败", null);
    }
}
